package vn.tbs.kcdk.fragments.contents;

import static vn.tbs.kcdk.global.Common.MEDIA_TYPE_AUDIO;
import static vn.tbs.kcdk.global.Common.MEDIA_TYPE_IMAGE;
import static vn.tbs.kcdk.global.Common.MEDIA_TYPE_VIDEO;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import vn.tbs.kcdk.KCDKActivity;
import vn.tbs.kcdk.R;
import vn.tbs.kcdk.fragments.contents.media.ContentFragment;
import vn.tbs.kcdk.fragments.contents.media.MediaInfo;
import vn.tbs.kcdk.global.Common;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class MediaListFragment extends ListFragment {
	private static final String TAG = MediaListFragment.class.getSimpleName();
	private MediaAdapter mMediaAdapter;
	private ListView mMediaContentListView;
	private List<MediaInfo> mMediaList;
	private ContentFragment mMediaContentFragment;
	private KCDKActivity mKCDKActivity;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mMediaContentListView = (ListView) inflater.inflate(R.layout.media_list, null);
		return mMediaContentListView;
	}

	
	@Override
	public void onResume() {
		Log.i(TAG, "onResume start");
		
		super.onResume();
		
		if (mKCDKActivity!=null) {
			mKCDKActivity.setmContentFragment(this);			
		}
		
		Log.i(TAG, "onResume end");
	}


	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mMediaList = loadMediaInfo();
		mMediaAdapter = new MediaAdapter(mMediaList,getActivity(),null);
		setListAdapter(mMediaAdapter);
		
		if (getActivity() == null)
			return;

		if (getActivity() instanceof KCDKActivity) {
			mKCDKActivity = (KCDKActivity) getActivity();
		}
	}

	private List<MediaInfo> loadMediaInfo() {
		Log.i(TAG, "loadCategoriesMenu start");

		List<MediaInfo> mediaList = new ArrayList<MediaInfo>();
		Random ran = new Random();
		int k = 0;
		for (int i = 0; i < 12; i++) {
			k = ran.nextInt(100);
			mediaList.add(new MediaInfo("00"+k,"thần điêu hiệp lữ "+k,"http:\\thandieuhieplu"+k+".mp3" ,MEDIA_TYPE_AUDIO));
		}
		
		Log.i(TAG, "loadCategoriesMenu end");
		return mediaList;
	}

	@Override
	public void onListItemClick(ListView lv, View view, int position, long id) {
		Log.i(TAG, "onListItemClick start" +position);

		boolean ok = switchContent(mMediaAdapter.getItem(position));
		if (ok) {
			mMediaAdapter.setSelectedPosition(position,true);			
		}
		
		Log.i(TAG, "onListItemClick end" +position);
	}

	private boolean switchContent(MediaInfo item) {
		Log.i(TAG, "switchContent start");

		if (item!=null) {
			switch (item.getMediaType()) {
			case MEDIA_TYPE_AUDIO:
				loadMediaContentFragment(item.getMediaId());
				return true;
			case MEDIA_TYPE_VIDEO:
				return true;
			case MEDIA_TYPE_IMAGE:
				return true;
			default:
				break;
			}
			
			
		}

		Log.i(TAG, "switchContent end");

		return false;
	}


	private void loadMediaContentFragment(String aMediaId) {
		Log.i(TAG, "loadMediaContentFragment start");
		
		mMediaContentFragment = new ContentFragment();
		Bundle arg = getArguments();
		arg.putString(Common.MEDIA_ID_KEY, aMediaId);
		mMediaContentFragment.setArguments(arg);
		mKCDKActivity.switchContent(mMediaContentFragment,true);

		Log.i(TAG, "loadMediaContentFragment end");
	}

	public void updateContent() {
		Log.i(TAG, "updateContent start");

		List<MediaInfo> mediaLists = loadMediaInfo();
		if (mMediaAdapter!=null) {
			mMediaAdapter.setMediaList(mediaLists );
			mMediaAdapter.notifyDataSetChanged();			
		}
		
		Log.i(TAG, "updateContent end");
	}
	
}
